
package net.swa.dewei.service.impl;

import net.swa.dewei.beans.entity.DianChi;
import net.swa.dewei.beans.entity.DianChiCheXingRelation;
import net.swa.dewei.service.DianChiService;
import net.swa.system.dao.HibernateDaoSupport;
import net.swa.util.JsonResult;
import java.util.List;
import org.hibernate.Query;
import org.springframework.stereotype.Service;


@Service("dianChiService")
public class DianChiServiceImpl extends HibernateDaoSupport implements DianChiService
{
    public boolean add(final DianChi m , final Long[] ids)
    {
        getCurrentSession().save(m);
        if (ids.length > 0)
        {
            for (int i = 0; i < ids.length; i++)
            {
                DianChiCheXingRelation r = new DianChiCheXingRelation();
                r.setDianchiId(m.getId());
                r.setChexingId(ids[i]);
                getCurrentSession().save(r);
            }
        }
        return true;
    }

    public boolean update(final DianChi m , final Long[] ids)
    {
        Query query = getCurrentSession().createQuery("delete from DianChiCheXingRelation where dianchiId=:id");
        query.setLong("id", m.getId());
        query.executeUpdate();
        getCurrentSession().update(m);
        if (ids.length > 0)
        {
            for (int i = 0; i < ids.length; i++)
            {
                DianChiCheXingRelation r = new DianChiCheXingRelation();
                r.setDianchiId(m.getId());
                r.setChexingId(ids[i]);
                getCurrentSession().save(r);
            }
        }
        return true;
    }

    public boolean delete(final Long[] ids)
    {
        Query query = null;
        for (int i = 0; i < ids.length; i++)
        {
            query = getCurrentSession().createQuery("delete from DianChiCheXingRelation where dianchiId=:id");
            query.setLong("id", ids[i]);
            query.executeUpdate();
            query = getCurrentSession().createQuery("delete from DianChi where id=:id");
            query.setLong("id", ids[i]);
            query.executeUpdate();
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    public DianChi queryById(final Long id)
    {
        DianChi m = (DianChi) getCurrentSession().get(DianChi.class, id);
        Query query = getCurrentSession().createQuery("from DianChiCheXingRelation where dianchiId=:id");
        query.setLong("id", id);
        List<DianChiCheXingRelation> l = query.list();
        m.setRelationList(l);
        return m;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<DianChi> queryDianchiByCheXingId(final Long id)
    {
        // 默认查询最大数量
        final int count = 10;
        Query query = null;
        String hql = "from DianChiCheXingRelation where 1=1 ";
        if (null != id)
        {
            hql += " and chexingId=:id";
        }
        query = getCurrentSession().createQuery(hql);
        if (null != id)
        {
            query.setLong("id", id);
        }
        List<DianChiCheXingRelation> l = query.list();
        hql = "from DianChi where 1=1 ";
        if (null != l && l.size() > 0)
        {
            String dianchiIds = "";
            for (int i = 0; i < l.size() - 1; i++)
            {

                dianchiIds = dianchiIds + l.get(i).getDianchiId() + ",";
            }
            dianchiIds = dianchiIds + l.get(l.size() - 1).getDianchiId();
            hql += " and id in(" + dianchiIds + ") ";
        }
        hql += " order by xingHao,pinPai,sort";
        query = getCurrentSession().createQuery(hql);
        // 最多查询记录数
        query.setMaxResults(count);
        List<DianChi> rlist = query.list();
        JsonResult<DianChi> json = new JsonResult<DianChi>();
        json.setResult(rlist);
        json.setSuccess(true);
        return json;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<DianChi> queryDianchis(final Long id , final String province , final String city , final String town)
    {
//        log.debug("查询电池，省：" + province + " ,市：" + city + " ,县：" + town);
        //默认查询最大数量
        final int count = 10;
        Query query = null;
        String sql = "select t.* from  t_DianChi t join t_DianChiCheXingRelation r on r.dianchiId=t.id  where 1=1 ";
        if (null != id)
        {
            sql += " and chexingId=:id";
        }
        if (null != province)
        {
            if (province.length() > 4)
            {
                sql += " and province  like :province";
            }
            else
            {
                sql += " and province=:province";
            }
        }
        if (null != city)
        {
            sql += " and city=:city";
        }
        if (null != town)
        {
            sql += " and town=:town";
        }
        sql += " order by sort";
        query = getCurrentSession().createSQLQuery(sql).addEntity("t", DianChi.class);
        if (null != id)
        {
            query.setLong("id", id);
        }
        if (null != province)
        {
            if (province.length() > 4)
            {
                query.setString("province", province.substring(0, 2) + "%");
            }
            else
            {
                query.setString("province", province);
            }

        }
        if (null != city)
        {
            query.setString("city", city);
        }
        if (null != town)
        {
            query.setString("town", town);
        }
        //最多查询记录数
        query.setMaxResults(count);
        List<DianChi> rlist = query.list();
        JsonResult<DianChi> json = new JsonResult<DianChi>();
        if (null != rlist && rlist.size() > 0)
        {
            json.setResult(rlist);
        }
        else
        {
            sql = "select t.* from  t_DianChi t join t_DianChiCheXingRelation r on r.dianchiId=t.id  where 1=1 ";
            if (null != id)
            {
                sql += " and chexingId=:id";
            }
            if (null != province)
            {
                if (province.length() > 4)
                {
                    sql += " and province  like :province";
                }
                else
                {
                    sql += " and province=:province";
                }
            }
            if (null != city)
            {
                sql += " and city=:city";
            }
            if (null != town)
            {
                sql += " and town like :town";
            }
            sql += " order by sort";
            query = getCurrentSession().createSQLQuery(sql).addEntity("t", DianChi.class);
            if (null != id)
            {
                query.setLong("id", id);
            }
            if (null != province)
            {
                if (province.length() > 4)
                {
                    query.setString("province", province.substring(0, 2) + "%");
                }
                else
                {
                    query.setString("province", province);
                }
            }
            if (null != city)
            {
                query.setString("city", city);
            }
            if (null != town && town.length() > 2)
            {
                query.setString("town", town.substring(0, town.length() - 1) + "%");
            }
            //最多查询记录数
            query.setMaxResults(count);
            rlist = query.list();
            
            json.setResult(rlist);
        }
        json.setSuccess(true);
        return json;

//        // 默认查询最大数量
//        final int count = 10;
//        Query query = null;
//        String sql = "select t.* from  t_DianChi t join t_DianChiCheXingRelation r on r.dianchiId=t.id  where 1=1 ";
//        if (null != id)
//        {
//            sql += " and chexingId=:id";
//        }
//        if (null != province)
//        {
//            sql += " and province=:province";
//        }
//        if (null != city)
//        {
//            sql += " and city=:city";
//        }
//        if (null != id)
//        {
//            sql += " and town=:town";
//        }
//        sql += " order by sort";
//        query = getCurrentSession().createSQLQuery(sql).addEntity("t", DianChi.class);
//        if (null != id)
//        {
//            query.setLong("id", id);
//        }
//        if (null != province)
//        {
//            query.setString("province", province);
//        }
//        if (null != city)
//        {
//            query.setString("city", city);
//        }
//        if (null != id)
//        {
//            query.setString("town", town);
//        }
//        // 最多查询记录数
//        query.setMaxResults(count);
//        List<DianChi> rlist = query.list();
//        JsonResult<DianChi> json = new JsonResult<DianChi>();
//        json.setResult(rlist);
//        json.setSuccess(true);
//        return json;
    }

    @Override
    public DianChi queryBypctpx(String province , String city , String town , String pinPai , String xingHao)
    {
        Query query = getCurrentSession().createQuery(" from DianChi where province=:province and city=:city and town=:town and pinPai=:pinPai and xingHao=:xingHao ");
        DianChi m=null;
        query.setString("province", province);
        query.setString("city", city);
        query.setString("town", town);
        query.setString("pinPai", pinPai);
        query.setString("xingHao", xingHao);
        if(null!=query.list()&&query.list().size()>0){
            m=(DianChi) query.list().get(0);
        }
        return m;
    }
}